package com.yutons.encrypt.config;

import com.yutons.encrypt.cryptor.Sm4MpEncrypt;
import com.yutons.encrypt.cryptor.IMpEncrypt;
import com.yutons.encrypt.interceptor.MpEncryptParameterInterceptor;
import com.yutons.encrypt.interceptor.MpEncryptResultSetInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @Title 数据安全starter启动器自动注入类配置
 * @Author yutons
 * @Date 2024-11-20 13:52
 * @Desc 注入EncryptConfig配置
 * 注入IEncryptor接口类
 */
@Configuration
@EnableConfigurationProperties({MpEncryptProperties.class})
public class MpEncryptConfiguration {
	@Resource
	private MpEncryptProperties properties;

	@PostConstruct
	public void initialize() {
		System.out.printf("=========   mp-encrypt  数据隐私安全-敏感数据\033[36m落库加密\033[0m组件：%s   =========%n",
			properties.getEnabled() ? "\033[31m已启用\033[0m" : "\033[32m未启用\033[0m");
	}

	@Bean
	@ConditionalOnMissingBean(Sm4MpEncrypt.class)
	public Sm4MpEncrypt customSm4Encryptor() {
		return new Sm4MpEncrypt(this.properties);
	}

	@Bean
	@ConditionalOnMissingBean(MpEncryptParameterInterceptor.class)
	public MpEncryptParameterInterceptor parameterInterceptor(@Autowired(required = false) IMpEncrypt customEncryptor) {
		return new MpEncryptParameterInterceptor(customEncryptor,properties);
	}

	@Bean
	@ConditionalOnMissingBean(MpEncryptResultSetInterceptor.class)
	public MpEncryptResultSetInterceptor resultSetInterceptor(@Autowired(required = false) IMpEncrypt customEncryptor) {
		return new MpEncryptResultSetInterceptor(customEncryptor,properties);
	}
}
